package com.mysoft.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysoft.common.entity.Role;
import com.mysoft.common.entity.User;
import com.mysoft.common.entity.UserRole;
import com.mysoft.dao.dao.UserRoleMapper;
import com.mysoft.service.service.RoleService;
import com.mysoft.service.service.UserRoleService;
import com.mysoft.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JohnCoding
 * @Description:
 * @date: 2019/12/23 15:18
 */
@Service
public class UserRoleSerivceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Override
    public List<Role> getRolesByUserName(String userName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userName);
        queryWrapper.last("limit 1");
        User user = userService.getOne(queryWrapper);
        if (null == user) {
            return null;
        }
        Long userId = user.getId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", userId);
        List<UserRole> list = baseMapper.selectList(wrapper);
        List<Role> result = new ArrayList<>();
        for (UserRole userRole : list) {
            Role role = roleService.getById(userRole.getRoleId());
            result.add(role);
        }
        return result;
    }
}
